#include "Socket.hpp"
#include "Common.hpp"
#include "Protocol.hpp"

void GetDataFromStdin(int *x, int *y, char *oper)
{
    std::cout << "Please Enter x: ";
    std::cin >> *x;
    std::cout << "Please Enter y: ";
    std::cin >> *y;
    std::cout << "Please Enter oper: ";
    std::cin >> *oper;
}

//  ./tcpclient server_ip server_port
int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        std::cerr << "Usage: " << argv[0] << " server_ip server_port" << std::endl;
        exit(USAGE_ERROR);
    }

    Enable_Console_Log_Strategy();

    std::string server_ip = argv[1];
    uint16_t server_port = std::stoi(argv[2]);

    // 1. 创建套接字
    // 2. 设置连接套接字
    std::shared_ptr<Socket> client = std::make_shared<TcpSocket>();
    client->BuildTcpClient(server_ip,server_port);

    auto protocol = std::make_unique<Protocol>();

    while (true)
    {
        int x, y;
        char oper;
        GetDataFromStdin(&x, &y, &oper);

        std::string request_string = protocol->BuildRequestString(x,y,oper);

        client->Send(request_string);

        Response resp;
        bool OK = protocol->GetResponse(client,&resp);
        if(!OK)
            break;
        resp.ShowResult();
    }

    client->Close();
    return 0;
}